﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using linkouqs.Gamekit;

public class PlayerInput : MonoBehaviour {

    public float moveSpeed = 8f;
    public float jumpSpeed = 16f;
    public float gravity = 40f;

    public KeyCode jumpKey = KeyCode.Z;

    bool inputJump;
    bool falling;
    Vector2 moveDirection = Vector2.zero;

    CharacterController2D controller;

    private void Start() {
        controller = GetComponent<CharacterController2D>();
    }

    private void Update() {
    //private void FixedUpdate() {

        /// 官方文档参考

        //float hor = Input.GetAxis("Horizontal");
        //if (controller.IsGrounded) {
        //    moveDirection = new Vector2(hor, 0);
        //    moveDirection = transform.TransformDirection(moveDirection);
        //    moveDirection.x *= moveSpeed;
        //    if (Input.GetKey(KeyCode.Z)) {
        //        moveDirection.y = jumpSpeed;
        //    }
        //}
        //moveDirection.y -= gravity * Time.deltaTime;
        //controller.Move(moveDirection * Time.deltaTime);

        /// 优化手感后

        if (controller.IsCeilinged) {
            if (!falling) {
                moveDirection.y = 0;
                falling = true;
            }
        }
        moveDirection.x = Input.GetAxis("Horizontal") * moveSpeed;
        if (controller.IsGrounded) {
            if (!inputJump) {
                if (Input.GetKey(jumpKey)) {
                    moveDirection.y = jumpSpeed;
                    inputJump = true;
                }
            }
            falling = false;
        } else {
            inputJump = false;
        }
        if(moveDirection.y > -1000f) {
            moveDirection.y -= gravity * Time.deltaTime;
        }
        controller.Move(moveDirection * Time.deltaTime);
    }

}
